
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'state.dart';

class UserCreateLogic extends GetxController {
  final state = UserCreateState();


  //请求发送验证码
  void sendSms(){
    var params = {
      "mobile":state.mobileNum,
      "verify_code_type": "signup"
    };
    Api.sendSms(params).listen((event) {
      state.smsData = event;
      toast("验证码已发送,请注意查收");
      update();
    });

    state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (state.seconds == 0) {
        state.timer?.cancel();
        state.verifyStr = '重新发送';
        state.seconds = 60;
        update();
        return;
      }
      state.seconds = --state.seconds;
      state.verifyStr = "已发送${state.seconds}s";
      print("已发送${state.seconds}s");
      update();
    });
  }

  void verifyRegistration(){
    var params = {
      "mobile":state.mobileNum,
      "sms_verify_type":"signup",
      "channel_num_id":99,
      "sms_verify_code":state.controller.text,
      "sms_verify_token": state.smsData["token"]
    };
    Api.registMember(params).listen((event) {
      if(event["code"] == 0){
        toast('注册成功');
        update();
        Get.back();
      }
    });
    //
    // state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
    //   if (state.seconds == 0) {
    //     state.timer?.cancel();
    //     state.verifyStr = '重新发送';
    //     state.seconds = 60;
    //     update();
    //     return;
    //   }
    //   state.seconds = --state.seconds;
    //   state.verifyStr = "已发送${state.seconds}s";
    //   print("已发送${state.seconds}s");
    //   update();
    // });
  }

  @override
  void dispose() {
    super.dispose();
    state.timer?.cancel();
  }
}
